package cn.apotato.inclusion.commin.response;

import cn.apotato.inclusion.commin.enums.InclusionStrategy;
import cn.apotato.inclusion.commin.regex.DateTimeRegex;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

/**
 * @author xphu
 * @version 1.0
 * @date 2021/8/6 14:23
 */
public class ResponseData<T> {

    private static ResponseMessage message;

    public static ResponseMessage success(String key, Object value, InclusionStrategy tiled){
        message = new ResponseMessage();
        message.success(key, value);
        message.tiled(tiled != null ? tiled : InclusionStrategy.WITH_KEY);
        return message;
    }

    public static ResponseMessage success(String key, Object value){
        message = new ResponseMessage();
        message.success(key, value);
        message.tiled(InclusionStrategy.WITH_KEY);
        return message;
    }

    public static ResponseMessage fail(){
        message = new ResponseMessage();
        message.fail();
        return message;
    }

    public static ResponseMessage fail(String msg){
        message = new ResponseMessage();
        message.fail(msg);
        return message;
    }

    public static ResponseMessage fail(int code, String msg){
        message = new ResponseMessage();
        message.fail(code, msg);
        return message;
    }

    public static ResponseMessage fail(String key, Object value, InclusionStrategy tiled){
        message = new ResponseMessage();
        if (value != null){
            message.fail(key, value);
        }
        message.tiled(tiled != null ? tiled : InclusionStrategy.WITH_KEY);
        return message;
    }

    public ResponseMessage timeStamp(){
        message.put(ResponseMessage.TIME_STAMP , LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
        return message;
    }

    public ResponseMessage dateTime(){
        message.put(ResponseMessage.DATE_TIME , LocalDateTime.now().format(DateTimeFormatter.ofPattern(DateTimeRegex.YYYY_MM_DD_HH_MM_SS)));
        return message;
    }

    /**
     *
     * @param pattern yyyy-MM-dd HH:mm:ss
     */
    public ResponseMessage dateTime(String pattern){
        message.put(ResponseMessage.DATE_TIME,
                LocalDateTime.now()
                        .format(DateTimeFormatter.ofPattern(StringUtils.isEmpty(pattern) ? DateTimeRegex.YYYY_MM_DD_HH_MM_SS : pattern)));
        return message;
    }

}
